package com.hq.ims.data.event.listener;

import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.LogUserOperation;
import com.hq.ims.data.entity.User;
import com.hq.ims.data.event.AddLogUserOperationEvent;
import com.hq.ims.data.service.BaseCompanyService;
import com.hq.ims.data.service.BaseUserService;
import com.hq.ims.data.vo.LogUserOperationBean;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.LocalDateTimeUtils;
import com.hq.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;

/**
 * 用户操作事件监听器
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class PushUserOperationLogListener {

    @Resource
    private BaseCompanyService baseCompanyService;

    @Resource
    private BaseUserService baseUserService;

    @Async
    @EventListener
    public void onApplicationEvent(AddLogUserOperationEvent addLogUserOperationEvent) {
        LogUserOperationBean logUserOperationBean = addLogUserOperationEvent.getLogUserOperation();
        if (logUserOperationBean == null) {
            log.info("日志实体为空,请联系管理员");
            return;
        }
        LogUserOperation logUserOperation = JsonUtils.convertObject(logUserOperationBean, LogUserOperation.class);
        logUserOperation.setParam(StringUtils.isEmpty(logUserOperation.getParam()) ? "无" : logUserOperation.getParam());
        //获取当前操作人用户的id
        String operationUserId = logUserOperationBean.getUserId();
        String realName = "未知用户";
        String userId = "未知";
        String account = "未知";
        //获取用户信息
        User user = baseUserService.getById(operationUserId);
        if (user != null) {
            realName = user.getRealName();
            userId = user.getId();
            account = user.getAccount();
        }
        //获取公司信息
        String companyId = StringUtils.isEmpty(logUserOperationBean.getCompanyId()) ? "无" : logUserOperationBean.getCompanyId();
        String companyName = "未知公司名称";
        Company company = baseCompanyService.getById(companyId);
        if (company != null) {
            companyName = company.getCompanyName();
        }
        //获取具体操作
        String operate = logUserOperationBean.getOperate();
        StringBuffer stringBuffer = new StringBuffer();
        //获取当前时间
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //拼接用户的操作
        String newOperationDetail = stringBuffer
                .append("来自: ")
                .append(companyName)
                .append(" 的 ")
                .append(realName)
                .append(" [id:")
                .append(userId)
                .append("] ，在 [")
                .append(LocalDateTimeUtils.toStr())
                .append("] 进行了 [")
                .append(StringUtils.isNotEmpty(operate) ? operate : "无")
                .append("] 操作")
                .append(" ，是否成功: ")
                .append(logUserOperationBean.getResult() ? "成功" : "失败")
                .append(" ，返回信息: \"")
                .append(StringUtils.ifEmptyDefault(logUserOperationBean.getMsg(), "无"))
                .append("\"")
                .toString();
        logUserOperation.setOperationDetail(newOperationDetail);
        logUserOperation.setUserAccount(account);
        boolean res = logUserOperation.insert();
        if (!res) {
            log.error("数据插入失败,请联系管理员");
        }
    }
}
